package com.qf.controller;
import com.baomidou.dynamic.datasource.aop.DynamicDataSourceAnnotationInterceptor;
import com.baomidou.dynamic.datasource.toolkit.DynamicDataSourceContextHolder;
import com.qf.Constant.AuthConstant;

import com.qf.Constant.OrderConstant;

import com.qf.entity.OrderEntity;
import com.qf.entity.User;
import com.qf.feign.AddressService;
import com.qf.feign.CarService;
import com.qf.intercetor.DynamicDataSource;
import com.qf.result.Result;
import com.qf.service.OrderService;
import com.qf.vo.Address;
import com.qf.vo.OrderParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ExecutionException;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 你的名字
 * @Date: 2021/07/19/19:37
 * @Description:
 */
@Slf4j
@Controller
@RequestMapping("/orderController")
public class orderController {
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private AddressService addressService;
    @Autowired
    private CarService carService;
    @Autowired
    private OrderService orderService;
    @Autowired
    private DynamicDataSource  dynamicDataSource;
    @RequestMapping("/{page}.html")
    public String toPage(@PathVariable String page, String gid, HttpSession session,ModelMap modelMap){
        List<String> carGidArray=new ArrayList<>();
        if (gid!=null) {
            String[] split = gid.split(",");
            carGidArray= Arrays.asList(split);
        }
        //获取当前用户
        User user = (User) session.getAttribute(AuthConstant.AUTH_SESSION);
        if (user==null) {
            return "redirect:http://localhost/auth/login.html";
        }
      //根据用户id查询收货地址
        List<Address> addressList = addressService.getAddressListByUid(user.getId());

        Map<String,Object> carMap=null;
      //查询用户购物车
        if (carGidArray.size()>0) {
           carMap= carService.getCarListByUidAndGid(user.getId(),carGidArray);
        }else {
           carMap= carService.getCarListByUid(user.getId());
        }
        modelMap.put("carMap",carMap);
        modelMap.put("addressList",addressList);
        return page;
    }
    //创建订单
   @RequestMapping("/createOrder")
    public String createOrder(HttpSession session, OrderParam orderParam) throws ExecutionException, InterruptedException {
        //查询用户
       User user = (User) session.getAttribute(AuthConstant.AUTH_SESSION);

       if(user==null){
           return "redirect:http://localhost/auth/login.html";
       }
       String oid = orderService.createOrder(orderParam, user.getId());
       return "redirect:http://localhost/pay/ali/pay?oid="+oid;
   }


    /**
     * 列表
     */
    @RequestMapping("/list")

    public Result list(@RequestParam Map<String, Object> params){

        return Result.success();
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{oid}")

    public Result info(@PathVariable("oid") String oid){
        OrderEntity order = orderService.getById(oid);

        return Result.success();
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public Result save(@RequestBody OrderEntity order){
        orderService.save(order);

        return Result.success();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public Result update(@RequestBody OrderEntity order){
        orderService.updateById(order);

        return Result.success();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    public Result delete(@RequestBody String[] oids){
        orderService.removeByIds(Arrays.asList(oids));

        return Result.success();
    }
    @RequestMapping("/getOrderByOid/{oid}")
    @ResponseBody
    public OrderEntity getOrderByOid(@PathVariable("oid") String oid){
        Integer tabIndex = dynamicDataSource.getTabIndex();
        OrderEntity order = orderService.getById(oid,tabIndex);
        return order;
    }
    @RequestMapping("/updateOrderStatus")
   @ResponseBody
    void updateOrderStatus(@RequestParam("oid") String oid,@RequestParam("status") Integer status){
          orderService.updateOrderStatus(oid,status);
    }
    @RequestMapping("/getInfoByOid/{oid}")
    public OrderEntity getInfoByOid(@PathVariable("oid") String oid){
        OrderEntity order = orderService.getInfoById(oid);
        return order;
    }

}
